package com.cskaoyan.controller.admin_promotion_controller;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.promotionVO.couponVO.CouponListVO;
import com.cskaoyan.bean.promotionVO.coupon_userVO.CouponUserVO;
import com.cskaoyan.service.promotionService.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;

@RestController
public class AdminCouponController {

    @Autowired
    CouponService couponService;


    @RequestMapping("/admin/coupon/list")
    public BaseRespVo couponList(Integer page, Integer limit, String sort, String order, String name, Short type, Short status) {
        CouponListVO couponListVO = couponService.couponList(page, limit, sort, order, name, type, status);

        return BaseRespVo.ok(couponListVO);
    }

    @RequestMapping("/admin/coupon/create")
    public BaseRespVo couponAdd(@RequestBody Coupon coupon) {
        //先判断设置时间是否合法
        if (coupon.getEndTime() != null && coupon.getDays() != 0) {
            return BaseRespVo.fail("只能设置一个优惠券有效期");
//        } else if (coupon.getEndTime() != null) {
//            Timestamp endTime = coupon.getEndTime();
//            Boolean flag = endTime.after(new Timestamp(System.currentTimeMillis()));
//            if (flag) {
//                Coupon couponVO = couponService.couponAdd(coupon);
//                return BaseRespVo.ok(couponVO);
//            } else {
//                return BaseRespVo.fail("优惠券结束时间设置错误");
//            }
        } else {
            Coupon couponVO = couponService.couponAdd(coupon);
            return BaseRespVo.ok(couponVO);
        }
    }


    @RequestMapping("/admin/coupon/delete")
    public BaseRespVo couponDel(@RequestBody Coupon coupon) {
        int code = couponService.couponDel(coupon);
        if (code == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }
    }

    @RequestMapping("/admin/coupon/update")
    public BaseRespVo couponUpdate(@RequestBody Coupon coupon) {
        //先判断设置有效期是否合法
        if (coupon.getEndTime() != null && coupon.getDays() != 0) {
            return BaseRespVo.fail("只能设置一个优惠券有效期");
        } else if (coupon.getEndTime() != null) {
            Timestamp endTime = coupon.getEndTime();
            Boolean flag = endTime.after(new Timestamp(System.currentTimeMillis()));
            if (flag) {
                Coupon couponVO = couponService.couponUpdate(coupon);
                return BaseRespVo.ok(couponVO);
            } else {
                return BaseRespVo.fail("优惠券结束时间设置错误");
            }
        } else {
            Coupon couponVO = couponService.couponUpdate(coupon);
            return BaseRespVo.ok(couponVO);
        }
    }

    @RequestMapping("/admin/coupon/read")
    public BaseRespVo couponRead(Integer id) {
        Coupon coupon = couponService.couponRead(id);
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("/admin/coupon/listuser")
    public BaseRespVo couponListUser(Integer page, Integer limit, String sort, String order, Integer couponId, Integer userId, Short status) {
        CouponUserVO couponUserVO = couponService.couponListUser(page, limit, sort, order, couponId, userId, status);
        return BaseRespVo.ok(couponUserVO);
    }

}
